package com.castle.fortress.admin.member.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.admin.member.entity.MemberEntity;
import com.castle.fortress.admin.member.entity.MemberGroupEntity;
import com.castle.fortress.admin.member.service.MemberGroupMembersService;
import com.castle.fortress.admin.member.service.MemberGroupService;
import com.castle.fortress.admin.member.service.MemberService;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.common.utils.CommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.entity.MemberAuthorityEntity;
import com.castle.fortress.admin.member.dto.MemberAuthorityDto;
import com.castle.fortress.admin.member.mapper.MemberAuthorityMapper;
import com.castle.fortress.admin.member.service.MemberAuthorityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 会员可见权限表 服务实现类
 *
 * @author castle
 * @since 2023-02-14
 */
@Service
public class MemberAuthorityServiceImpl extends ServiceImpl<MemberAuthorityMapper, MemberAuthorityEntity> implements MemberAuthorityService {
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberGroupMembersService memberGroupMembersService;
    @Autowired
    private MemberGroupService memberGroupService;
    /**
    * 获取查询条件
    * @param memberAuthorityDto
    * @return
    */
    public QueryWrapper<MemberAuthorityEntity> getWrapper(MemberAuthorityDto memberAuthorityDto){
        QueryWrapper<MemberAuthorityEntity> wrapper= new QueryWrapper();
        if(memberAuthorityDto != null){
            MemberAuthorityEntity memberAuthorityEntity = ConvertUtil.transformObj(memberAuthorityDto,MemberAuthorityEntity.class);
            wrapper.like(memberAuthorityEntity.getId() != null,"id",memberAuthorityEntity.getId());
            wrapper.like(memberAuthorityEntity.getType() != null,"type",memberAuthorityEntity.getType());
            wrapper.like(StrUtil.isNotEmpty(memberAuthorityEntity.getMemberId()),"member_id",memberAuthorityEntity.getMemberId());
            wrapper.like(StrUtil.isNotEmpty(memberAuthorityEntity.getGroupId()),"group_id",memberAuthorityEntity.getGroupId());
            wrapper.like(StrUtil.isNotEmpty(memberAuthorityEntity.getDeptId()),"dept_id",memberAuthorityEntity.getDeptId());
            wrapper.like(StrUtil.isNotEmpty(memberAuthorityEntity.getTagId()),"tag_id",memberAuthorityEntity.getTagId());
            wrapper.like(memberAuthorityEntity.getImposeId() != null,"impose_id",memberAuthorityEntity.getImposeId());
        }
        return wrapper;
    }


    @Override
    public IPage<MemberAuthorityDto> pageMemberAuthority(Page<MemberAuthorityDto> page, MemberAuthorityDto memberAuthorityDto) {
        QueryWrapper<MemberAuthorityEntity> wrapper = getWrapper(memberAuthorityDto);
        Page<MemberAuthorityEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberAuthorityEntity> memberAuthorityPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberAuthorityDto> pageDto = new Page(memberAuthorityPage.getCurrent(), memberAuthorityPage.getSize(),memberAuthorityPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(memberAuthorityPage.getRecords(),MemberAuthorityDto.class));
        return pageDto;
    }


    @Override
    public List<MemberAuthorityDto> listMemberAuthority(MemberAuthorityDto memberAuthorityDto){
        QueryWrapper<MemberAuthorityEntity> wrapper = getWrapper(memberAuthorityDto);
        List<MemberAuthorityEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,MemberAuthorityDto.class);
    }

    @Override
    public Map<String, Object> scope(Integer authorityType, Long imposeId, SysUser sysUser) {
        Map<String,Object> scopeMap=new HashMap<>();
        MemberAuthorityDto authorityDTO=getByImposeId(authorityType,imposeId,sysUser);
        if(authorityDTO!=null){
            scopeMap.put("memberGroups",authorityDTO.getGroupId());
            scopeMap.put("members",authorityDTO.getMemberId());
        }
        return scopeMap;
    }

    @Override
    public MemberAuthorityDto getByImposeId(Integer authorityType, Long imposeId,SysUser sysUser) {
        if(authorityType==null || imposeId==null){
            return null;
        }
        QueryWrapper<MemberAuthorityEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",authorityType);
        queryWrapper.eq("impose_id",imposeId);
        List<MemberAuthorityEntity> authorityEntities=baseMapper.selectList(queryWrapper);
        if(authorityEntities==null || authorityEntities.isEmpty()){
            return null;
        }
        MemberAuthorityDto authorityDTO= ConvertUtil.transformObj(authorityEntities.get(0),MemberAuthorityDto.class);
        if(StrUtil.isNotBlank(authorityDTO.getMemberId())){
            StringBuilder memberNames=new StringBuilder();
            List<String> memberIds = Arrays.asList(authorityDTO.getMemberId().split(","));
            List<Long> list = new ArrayList<>();
            for(String id:memberIds){
                list.add(Long.parseLong(id));
            }
            List<MemberEntity> memberDTOS=memberService.listByIds(list);
            for(MemberEntity entity:memberDTOS){
                if (list.contains(entity.getId())){
                    memberNames.append(entity.getNickName()+",");
                }
            }
            authorityDTO.setMemberName(memberNames.toString());
        }
        if(StrUtil.isNotEmpty(authorityDTO.getGroupId())){
            StringBuilder groupNames=new StringBuilder();
            List<String> groupIds = Arrays.asList(authorityDTO.getGroupId().split(","));
            List<Long> list = new ArrayList<>();
            for(String id:groupIds){
                list.add(Long.parseLong(id));
            }
            List<MemberGroupEntity> memberGroupDTOS=memberGroupService.listByIds(list);
            for(MemberGroupEntity entity:memberGroupDTOS){
                if (list.contains(entity.getId())){
                    groupNames.append(entity.getName()+",");
                }
            }
            authorityDTO.setGroupName(groupNames.toString());
        }
        return authorityDTO;
    }

    @Override
    public Map<String, Object> getAuthorityQueryMap(Integer code, Long memberId) {
        Map<String,Object> params=new HashMap<>();
        params.put("type",code);
        params.put("memberId",memberId);
        List<Long> groups = memberGroupMembersService.distinctGroupByMember(memberId);
        if(groups!=null && !groups.isEmpty()){
            params.put("groupId",groups);
        }
        return params;
    }

    @Override
    public Boolean checkUserAuthByPoseId(Integer authorityType, Long imposeId, Long memberId) {
        if(CommonUtil.verifyParamNull(authorityType,imposeId,memberId)){
            return false;
        }
        Map<String,Object> params=getAuthorityQueryMap(authorityType,memberId);
        params.put("imposeId",imposeId);
        List<MemberAuthorityEntity> list=baseMapper.queryAuth(params);
        return (list!=null && list.size()>0)?true:false;
    }
}

